<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直居中</title>
    <style>
        /* 1)absolute + 负margin */
        /* .parent {
            border: 1px red solid;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .child {
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
        } */

        /* 2）absolute + margin auto */
        /* .parent {
            border: 1px red solid;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .child {
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        } */
        /* 3）absolute + calc函数 */
        /* .parent {
            border: 1px red solid;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .child {
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            top: calc(50% - 50px);
            left: calc(50% - 50px);
        } */

        /* 1）absolute + transform、 */
        /* .parent {
            border: 1px red solid;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .child {
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        } */

        /* flex布局 */
        .parent {
            display: flex;
            border: 1px red solid;
            width: 300px;
            height: 300px;
            justify-content: center;
            align-items: center;
        }

        .child {
            width: 100px;
            height: 100px;
            background-color: green;
        }

        /* grid布局 */
        .parent {
            border: 1px red solid;
            width: 300px;
            height: 300px;
            display: grid;
            /* 第一种设置 */
            /* 是整个内容区域在容器里面的水平位置（左中右） */
            justify-content: center;
            /* 是整个内容区域的垂直位置（上中下） */
            align-content: center;
        }

        .child {
            width: 100px;
            height: 100px;
            background-color: green;
            /* 第二种设置 */
            /*
            设置单元格内容的垂直位置（上中下） 
            align-self: center;
            设置单元格内容的水平位置（左中右）
            justify-self: center; */
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">
            child
        </div>
    </div>

</body>

</html>